#!/bin/bash

# 设置nacos服务地址
NACOS_SERVER="http://localhost:8848/nacos"

# 设置配置组和命名空间
CONFIG_GROUP="DEFAULT_GROUP"
CONFIG_NAMESPACE="public"

# 等待nacos服务完全启动
echo "等待nacos服务启动..."
sleep 5

# 发送provider-service的配置到nacos
echo "正在发送provider-service的配置到nacos..."
PROVIDER_CONFIG_FILE="provider-service/src/main/resources/yaml-config/application.yaml"

# 使用正确的API格式发送配置
curl -X POST "$NACOS_SERVER/v1/cs/configs" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "dataId=provider-service.yaml" \
  --data-urlencode "group=$CONFIG_GROUP" \
  --data-urlencode "namespaceId=$CONFIG_NAMESPACE" \
  --data-urlencode "content@$PROVIDER_CONFIG_FILE"

if [ $? -eq 0 ]; then
  echo "provider-service配置发送请求成功！"
else
  echo "provider-service配置发送请求失败！"
fi

# 发送consumer-service的配置到nacos
echo "正在发送consumer-service的配置到nacos..."
CONSUMER_CONFIG_FILE="consumer-service/src/main/resources/yaml-config/application.yaml"

# 使用正确的API格式发送配置
curl -X POST "$NACOS_SERVER/v1/cs/configs" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "dataId=consumer-service.yaml" \
  --data-urlencode "group=$CONFIG_GROUP" \
  --data-urlencode "namespaceId=$CONFIG_NAMESPACE" \
  --data-urlencode "content@$CONSUMER_CONFIG_FILE"

if [ $? -eq 0 ]; then
  echo "consumer-service配置发送请求成功！"
else
  echo "consumer-service配置发送请求失败！"
fi

# 验证配置是否成功发送
echo "\n验证配置是否成功发送到nacos..."

# 验证provider配置
echo "\n验证provider-service配置："
curl -X GET "$NACOS_SERVER/v1/cs/configs?dataId=provider-service.yml&group=$CONFIG_GROUP&namespaceId=$CONFIG_NAMESPACE"

# 验证consumer配置
echo "\n\n验证consumer-service配置："
curl -X GET "$NACOS_SERVER/v1/cs/configs?dataId=consumer-service.yml&group=$CONFIG_GROUP&namespaceId=$CONFIG_NAMESPACE"

echo "\n\n配置发送脚本执行完毕！"